package 代码随想录.数组;

public class a209长度最小的子数组 {
    static class Solution {
        public int minSubArrayLen(int target, int[] nums) {
            int i = 0;
            int j = 0;
            int ans = Integer.MAX_VALUE;
            int sum = 0;
            while (i <= j && j < nums.length) {
                sum += nums[j];
                while (sum >= target) {
                    ans = Math.min(j - i + 1, ans);
                    sum -= nums[i++];
                }
                j++;
            }
            return ans == Integer.MAX_VALUE ? 0 :ans;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int target = 7;
        int[] nums = {2, 3, 1, 2, 4, 3};
        System.out.println(solution.minSubArrayLen(target, nums));
    }
}
